package com.quyunshuo.module.home.view

import android.animation.TypeEvaluator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.util.Log
import android.view.View
import com.quyunshuo.androidbaseframemvvm.common.ext.px

val provinces = arrayOf(
    "北京市","湛江市","雷州市","上海市"
)

class ProvinceView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
    var province = "北京市"
        set(value) {
            field = value
            invalidate()
        }

    val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        textSize = 20f.px
    }

    override fun onDraw(canvas: Canvas) {
        canvas.drawText(province,width/2f,height/2f,paint)
    }

}
class ProvinceEvaluatort:TypeEvaluator<String>{
    override fun evaluate(fraction: Float, startValue: String?, endValue: String?): String {//这个方法，执行很多次
        //fraction：时间比例（0~1），如 0.4 表示总时长的 40%。
        val startIndex = provinces.indexOf(startValue)
        val endIndex = provinces.indexOf(endValue)

        val currentIndex = startIndex +((endIndex-startIndex)*fraction).toInt()
        Log.d("evaluate", "evaluate: $currentIndex $startIndex $endIndex $fraction")
        return provinces[currentIndex]
    }

}